package com.zhl.study.service.impl;

import com.zhl.study.service.IDatasourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * DynamicDataSourceService.java
 * @Description
 * @Author zhaol@leadsea.net
 * @Date 2024/9/3 下午5:39
 **/
@Service("currentDatasourceService")
public class CurrentDatasourceServiceImpl implements IDatasourceService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<String> getTableNames() {
        return jdbcTemplate.queryForList("SHOW TABLES", String.class);
    }

    @Override
    public List<String> fields(String tableName) {
        String sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + tableName + "'";
        return jdbcTemplate.queryForList(sql, String.class);
    }

    @Override
    public List<Map<String, Object>> executeQuery(String sql) {
        return jdbcTemplate.query(sql, new CamelCaseMapRowService());
    }
}
